<?php

//防止恶意调用
if (!defined('IN_MOD'))
{
    die('Hacking attempt');
}
if(PHP_VERSION >= '5.1')date_default_timezone_set('PRC');
error_reporting(E_ALL ^ (E_NOTICE | E_WARNING));
define('MODULEPARA_PATH',str_replace('\\','/',realpath(dirname(dirname(__FILE__)).'/'))."/");
require(MODULEPARA_PATH . '/config/version.php');
require(MODULEPARA_PATH . '/modulepara-'.VERSION_NUMBER.'/moduleaq.php');// 加载安全过滤
require(MODULEPARA_PATH . '/config/load/configdb.php');
require(MODULEPARA_PATH . '/modulepara-'.VERSION_NUMBER.'/service/MysqliService.class.php');
global $cjdb,$moduledb;
$moduledb=$cjdb=new ModuleMysqli($configdbi['host'],$configdbi['user'],$configdbi['pass'],'utf8',$configdbi['db']);

include MODULEPARA_PATH . '/modulepara-'.VERSION_NUMBER.'/service/PageService.class.php';// 分页类

//＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝//

// 获取列表分页HTML
// $pagehtml = getpagehtml(100, 10, 1, "");
// 新增分页调用 DATE：2020-1-10
function getpagehtml($total=0, $showrow=0, $curpage=0, $url='', $show_pages=2)
{
	$pagehtml = "";
	
	// 总记录数大于每页显示数，显示分页
	if($total > $showrow)
	{
		$modulepage = new modulepage($total, $showrow, $curpage, $url, $show_pages);
		$pagehtml .= $modulepage->myde_write("modulepage");
	}	
	
	return $pagehtml;
}

// 获取当前分类的顶级分类 支持三级分类查询
function gettopclass($tablename='', $classid=0, $classidname='', $bigclassname='', $colseTop = 0)
{
	global $cjdb;
	
	if($colseTop < 1)$modulewhere = "index_num != '自定义区块' and index_num != '自定义配置' and index_num != '自定义扩展' and";// 关闭顶级分类为自定义区块和自定义配置
	$sql="select * from $tablename where $modulewhere $classidname = '$classid'";
	$arr=$cjdb->getRow($sql);
	
	if(!empty($arr[$bigclassname]))
	{
		$sql2="select * from $tablename where $modulewhere $classidname = '$arr[$bigclassname]'";
		$arr2=$cjdb->getRow($sql2);

		if(is_array($arr2))
		{
			if(!empty($arr2[$bigclassname]))
			{
				$sql3="select * from $tablename where $modulewhere $classidname = '$arr2[$bigclassname]'";
				$arr3=$cjdb->getRow($sql3);
				
				if(is_array($arr3))
				{
					return $arr3[$classidname];
				}else{
					return $arr2[$classidname];		
				}
				
			}else{
				return $arr2[$classidname];	
			}
		}else{
			return $arr[$classidname];
		}
	
	}else{
		$returnID = !empty($arr) ? $arr[$classidname] : 0;
		return $returnID;
	}
}

// 获取当前分类的所有上级分类 支持三级分类查询
function getparentclass($tablename='', $classid=0, $classidname='', $bigclassname='')
{
	global $cjdb;
	
	$modulewhere = "index_num != '自定义区块' and index_num != '自定义配置' and index_num != '自定义扩展' and";// 关闭顶级分类为自定义区块和自定义配置
	$sql="select * from $tablename where $modulewhere $classidname = '$classid'";
	$arr=$cjdb->getRow($sql);
	
	if(!empty($arr[$bigclassname]))
	{
		$sql2="select * from $tablename where $modulewhere $classidname = '$arr[$bigclassname]'";
		$arr2=$cjdb->getRow($sql2);

		if(is_array($arr2))
		{
			if(!empty($arr2[$bigclassname]))
			{
				$sql3="select * from $tablename where $modulewhere $classidname = '$arr2[$bigclassname]'";
				$arr3=$cjdb->getRow($sql3);
				
				if(is_array($arr3))
				{
					return array("class1" => $arr3, "class2" => $arr2, "class3" => $arr, "classtype" => 3);	
				}else{
					return array("class1" => $arr2, "class2" => $arr, "classtype" => 2);	
				}
				
			}else{
				return array("class1" => $arr2, "class2" => $arr, "classtype" => 2);	
			}
		}else{
			return array("class1" => $arr, "classtype" => 2);	
		}
	
	}else{
		return array("class1" => $arr, "classtype" => 1);	
	}
}

// 合并当前一层分类组与二层分类组
function getTwoClassGroup($list=array(), $list2=array(), $classidname="", $bigclassname="")
{
	// 归类二级分类组
	$ids2 = "";
	$resList2 = array();
	foreach($list2 as $key2=>$val2)
	{
		$ids2 .= ",".$val2[$classidname];
		$resList2[$val2[$bigclassname]][] = $val2;
	}
	
	// 合并一级分类组和二级分类组
	$ids1 = "";
	$resList = array();
	foreach($list as $key=>$val)
	{
		$ids1 .= ",".$val[$classidname];
		$resList[$key] = $val;
		if($resList2[$val[$classidname]])
		{
			$resList[$key]["list2"] = $resList2[$val[$classidname]];
		}
	}
	
	$ids = delstrq($ids1.$ids2);
	
	return array("list" => $resList, "ids" => $ids);
}

// 获取当前分类组ID集
function getClassGroupIDS($list=array(), $classidname="")
{
	$ids = "";
	foreach($list as $key=>$val)
	{
		$ids .= ",".$val[$classidname];
	}
	
	$ids = delstrq($ids);
	
	return $ids;	
}

// 获取当前分类的所有下级分类 支持三级分类查询
function getbottomclass($tablename='', $classid=0, $classidname='', $bigclassname='', $idsok = 0, $where = "")
{
	global $cjdb;
	
	if($where)$whereok = "and $where";
	$sql="select * from $tablename where $bigclassname in($classid) $whereok";
	$list=$cjdb->getAll($sql);
	if(!empty($list))
	{
		$ids1 = getClassGroupIDS($list, $classidname);
		$sql="select * from $tablename where $bigclassname in($ids1) $whereok";
		$list2=$cjdb->getAll($sql);
	}
	
	// 重组一级分类组和二级分类组
	$ids = $classid;
	$catelist = array();
	if(!empty($list))
	{
		if(!empty($list2))
		{
			// 合并一层分类组和二层分类组
			$twoClassGroup = getTwoClassGroup($list, $list2, $classidname, $bigclassname);
			$catelist = $twoClassGroup['list'];
			$ids .= ",".$twoClassGroup['ids'];
		}else{
			$catelist = $list;
			$ids .= ",".$ids1;
		}	
	}
		
	// 统计层级分类总数
	$listdata = array();
	$listdata['count'] = count($catelist);
	$listdata['list'] = $catelist;
	foreach($listdata['list'] as $key=>$val)
	{
		if($val['list2'])
		{
			$listdata['list'][$key]['count'] = count($val['list2']);
		}else{
			$listdata['list'][$key]['count'] = 0;	
		}
	}
	
	if($idsok == 1)
	{
		$resdata = $ids;	
	}else{
		$resdata = array('list' => $listdata, 'ids' => $ids);	
	}
	
	return $resdata;
}

// 获取当前分类的分类信息，为空时自动获取上级分类信息 支持三级分类查询
function getclassinfo($tablename='', $classid=0, $classidname='', $bigclassname='', $infofield='')
{
	global $cjdb;
	
	$modulewhere = "index_num != '自定义区块' and index_num != '自定义配置' and index_num != '自定义扩展' and";// 关闭顶级分类为自定义区块和自定义配置
	$sql="select * from $tablename where $modulewhere $classidname = '$classid'";
	$arr=$cjdb->getRow($sql);
	
	if(!empty($arr[$bigclassname]) && empty($arr[$infofield]))
	{
		$sql2="select * from $tablename where $modulewhere $classidname = '$arr[$bigclassname]'";
		$arr2=$cjdb->getRow($sql2);

		if(is_array($arr2))
		{
			if(!empty($arr2[$bigclassname]) && empty($arr[$infofield]))
			{
				$sql3="select * from $tablename where $modulewhere $classidname = '$arr2[$bigclassname]'";
				$arr3=$cjdb->getRow($sql3);
				
				if(is_array($arr3))
				{
					return $arr3;
				}else{
					return $arr2;		
				}
				
			}else{
				return $arr2;	
			}
		}else{
			return $arr;
		}
	
	}else{
		return $arr;	
	}
}
?>